﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AutoFacUtils;
using FluentValidation;
using CashFlow.Web.Common;
using CashFlow.Web.Service;
using CashFlow.Web.Areas.ShopManage.Models.User;
using IUserService2 = CashFlow.Web.Areas.ShopManage.Service.IUserService;

namespace CashFlow.Web.Areas.ShopManage.Validator.User
{
    public class UpdateUserReferenceModelValidator
        : AbstractValidator<UpdateUserReferenceModel>
    {
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IUserService2 GetUserService2() { return AutoFacHelper.Get<IUserService2>(); }

        public UpdateUserReferenceModelValidator()
        {
            CascadeMode = CascadeMode.Stop;
            RuleFor(x => x.UserId).Must(x => CommonValidator.IsUserInShop(x))
                .WithMessage("非法UserId");
            RuleFor(x => x.ReferenceInvitationCode).Must((x, y) => IsReferenceInvitationCodeInShop(x))
                .WithMessage("非法ReferenceInvitationCode");
        }

        private bool IsReferenceInvitationCodeInShop(UpdateUserReferenceModel model)
        {
            var userService = GetUserService();
            var ui = userService.GetLoginShopInfo();
            var userService2 = GetUserService2();
            var bSuccess = userService2.IsReferenceInvitationCodeInShop(
                                ui.ShopManagerUserId
                                , model.UserId
                                , model.ReferenceInvitationCode
                           );
            return bSuccess;
        }
    }
}